import fetcher from "../apis/Fetcher";
import {Authority} from "./RoleApi";
import {NumberIdAndName} from "../types/BasicModels";
import {Page, Pageable} from "../utils/Page";
import {OffsetDateTime} from "@js-joda/core";

export const MAX_PASSWORD_LENGTH = 16;
export const INITIAL_PASSWORD_LENGTH = 8;
export const MIN_PASSWORD_LENGTH = INITIAL_PASSWORD_LENGTH;

export function getMe() {
    return fetcher.get<MeResponse>("employees/me")
}

export interface MeResponse {
    id:number
    name: string
    authorities: Array<Authority>
    systemService: {
        id:string,
        name:string
    }
}


export interface Role {
    id: number
    name: string
}

export const SUPER_ADMIN_ROLE_NAME = "超级管理员"

interface QueryEmployeeRequest {
    phoneNumber?: string
    name?: string
    roleIds?: number[]
    marketName?:string
    searchInput?:string
}

export function queryEmployees(pageable: Pageable, request: QueryEmployeeRequest) {
    return fetcher.get<Page<EmployeeResponse>>("employees", {...pageable.toRequestParams(), ...request})
}

export interface EmployeeResponse {
    id: number
    phoneNumber: string
    name: string
    createdDateTime: OffsetDateTime
    roles: Array<Role>
}

export interface Role {
    id: number
    name: string
}

// export const SUPER_ADMIN_ROLE_NAME = "超级管理员"

export interface CreateEmployeeRequest {
    phoneNumber: string
    name: string
    password?: string
    roleIds: number[]
}

export function createEmployee(request: CreateEmployeeRequest) {
    return fetcher.post<CreateEmployeeResponse>("employees", {}, request)
}

export interface CreateEmployeeResponse {
    password: string
}

export interface UpdateEmployeeRequest{
    phoneNumber?: string
    name?: string
    roleIds?: number[]
}

export function updateEmployee(id:number,request:UpdateEmployeeRequest){
    return fetcher.put<UpdateEmployeeRequest>(`employees/${id}`,{},request)
}

export function getAllEmployeeSummariesSortBySaleManager(){
    return fetcher.get("employees",{
        projection:"summary",
        sort:"saleManager"
    })
}

export function getEmployeeSummaries(){
    return fetcher.get<NumberIdAndName[]>("employees",{
        projection:"summary",
    })
}

export function getEmployeeDetail(id:number){
    return fetcher.get<EmployeeDetailResponse>(`employees/${id}`)
}

export interface EmployeeDetailResponse {
    id: number
    phoneNumber: string
    name: string
    createdDateTime: OffsetDateTime
    roles: Array<Role>
    createdBy?:NumberIdAndName
}
